package com.bocai.controller;

import com.bocai.bean.SysTabManager;
import com.bocai.service.ITabService;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.ObjUtils;
import com.bocai.utils.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Cemetery on 2018-04-18.
 * 标签管理
 */

@Controller
@RequestMapping("customTab")
public class TabController {
    @Autowired
    private ITabService TabService;

    @ApiOperation(value = "设置标签", notes = "增加、修改标签")
    @RequestMapping(value = "/setTab", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "tabId", dataType = "integer", required = false, value = "标签id", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "tabName", dataType = "string", required = false, value = "标签名", defaultValue = "唐唐"),
            @ApiImplicitParam(paramType = "query", name = "tabType", dataType = "integer", required = false, value = "标签类型（0，赛事 1，比赛）", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "isEnable", dataType = "integer", required = false, value = "是否封禁（0，开启 1，封禁）", defaultValue = "0"),
    })
    @ResponseBody
    public ResponseData setTab(Integer tabId, String tabName, Integer tabType, Integer isEnable) {
        ResponseData data = new ResponseData();
        data.setCodeEnum(CodeEnum.CODE_1);

        SysTabManager tabManager = new SysTabManager();
        tabManager.setTabName(tabName);
        tabManager.setTabType(tabType);
        tabManager.setIsEnable(isEnable);

        if (ObjUtils.isNotEmpty(tabId)) {
            tabManager.setTabId(tabId);
            if (TabService.updateTab(tabManager) > 0) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            }
        } else {
            if (TabService.insertTab(tabManager) > 0) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            }
        }
        return data;
    }

    @ApiOperation(value = "获取标签列表", notes = "五大联赛")
    @RequestMapping(value = "/getTabList", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "tabName", dataType = "string", required = false, value = "标签名", defaultValue = "唐唐"),
            @ApiImplicitParam(paramType = "query", name = "tabType", dataType = "integer", required = false, value = "标签类型（0，赛事 1，比赛）", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "isEnable", dataType = "integer", required = false, value = "是否封禁（0，开启 1，封禁）", defaultValue = "0"),
    })
    @ResponseBody
    public ResponseData getTabList(String tabName, Integer tabType, Integer isEnable) {
        ResponseData data = new ResponseData();

        SysTabManager tabManager = new SysTabManager();
        tabManager.setTabName(tabName);
        tabManager.setTabType(tabType);
        tabManager.setIsEnable(isEnable);

        List<SysTabManager> tabs = TabService.selectTab(tabManager);
        data.setCodeEnum(CodeEnum.SUCCESS);
        data.setResults(tabs);
        return data;
    }

    @ApiOperation(value = "查询标签列表", notes = "五大联赛")
    @RequestMapping(value = "/getTabs", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "tabIds", dataType = "string", required = true, value = "标签id(数组)", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData getTabs(String tabIds) {
        ResponseData data = new ResponseData();
        data.setCodeEnum(CodeEnum.CODE_1);
        String[] ids;
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{tabIds});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        } else {
            List<SysTabManager> tabs = TabService.selectTabs(tabIds);
            data.setCodeEnum(CodeEnum.SUCCESS);
            data.setResults(tabs);
        }
        return data;
    }

    @ApiOperation(value = "删除标签", notes = "删除")
    @RequestMapping(value = "/deleteTab", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "tabId", dataType = "int", required = false, value = "标签id", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData deleteTab(Integer tabId) {
        ResponseData data = new ResponseData();
        data.setCodeEnum(CodeEnum.CODE_1);
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{tabId});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        } else if (TabService.deleteTab(tabId) > 0) {
            data.setCodeEnum(CodeEnum.SUCCESS);
        } else
            data.setCodeEnum(CodeEnum.CODE_6);
        return data;
    }
}
